/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.parser.markup.types;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import moxl.parser.markup.Markup;
import moxl.parser.markup.MarkupException;
import moxl.parser.model.ParsedAttribute;
import moxl.tokenizer.Token;

public class OpenElementTagMarkup implements Markup
{
	Token elementNameToken;
	Map<String, ParsedAttribute> attributes;
	List<ParsedAttribute> attributesAsList;
	boolean isEmpty;

	public OpenElementTagMarkup(Token elementNameToken)
	{
		this.elementNameToken = elementNameToken;
	}

	public String getTagName()
	{
		return elementNameToken.getImage();
	}

	public void setTagName(String name)
	{
		elementNameToken.setImage(name);
	}

	public boolean isEmpty()
	{
		return isEmpty;
	}

	public void setEmpty(boolean isEmpty)
	{
		this.isEmpty = isEmpty;
	}

	public String getAttributeValue(String name)
	{
		if (attributes == null)
		{
			return null;
		}
		ParsedAttribute attribute = attributes.get(name);
		if (attribute == null)
		{
			return null;
		}
		if (attribute.getValue() == null)
		{
			return "";
		}
		return attribute.getValue();
	}

	public void addAttribute(Token attrNameToken, Token attrValueToken) throws MarkupException
	{
		if (attributes == null)
		{
			attributes = new HashMap<String, ParsedAttribute>();
			attributesAsList = new ArrayList<ParsedAttribute>();
		}
		String attributeName = attrNameToken.getImage();
		if (attributes.containsKey(attributeName))
		{
			throw new MarkupException(this, "Duplicate attribute named " + attrNameToken.getImage(), attrNameToken.getLocation());
		}
		String attributeValue = attrValueToken == null ? "" : attrValueToken.getImage();
		ParsedAttribute attribute = new ParsedAttribute(attributeName, attributeValue);
		attributes.put(attributeName, attribute);
		attributesAsList.add(attribute);
	}

	public Token getElementNameToken()
	{
		return elementNameToken;
	}

	// TODO: This is implemented incorrectly, should return first token (<) of markup!
	public Token getFirstToken()
	{
		return elementNameToken;
	}

	public int getNumberOfAttributes()
	{
		return attributes.size();
	}

	public boolean hasAttribute(String name)
	{
		if (attributes == null)
		{
			return false;
		}
		return attributes.containsKey(name);
	}

	public Collection<ParsedAttribute> getAttributes()
	{
		if (attributesAsList == null)
		{
			return Collections.emptyList();
		}
		return attributesAsList;
	}
}
